package com.qf.fengmimall.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.fengmimall.product.entity.ProductParams;
import com.qf.fengmimall.product.mapper.ProductParamsMapper;
import com.qf.fengmimall.product.service.IProductParamsService;
import com.qf.fengmimall.rabbitmq.ProductParamProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商品参数  服务实现类
 * </p>
 *
 * @author lx
 * @since 2023-02-15
 */
@Service
public class ProductParamsServiceImpl extends ServiceImpl<ProductParamsMapper, ProductParams> implements IProductParamsService {

    @Autowired
    private ProductParamProducer productParamProducer;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addProduct(ProductParams productParams) throws JsonProcessingException {
        //将添加商品信息转为json格式发mq，存入es
        ObjectMapper objectMapper = new ObjectMapper();
        String productParamsToJson = objectMapper.writeValueAsString(productParams);
        productParamProducer.send("product_exchange", "product.info.params", productParamsToJson);
        save(productParams);
    }
}
